import { request, useUserStore, clearEmpty } from "jmash-core";
import qs from "qs";
import type { AxiosPromise } from "axios";
import type {
  DictRegionApi,
  DictRegionReq,
  DictRegionModel,
  DictRegionList,
  DictRegionKey,
  DictRegionMoveKey,
  DictRegionEnableKey,
  DictRegionCreateReq,
  DictRegionUpdateReq,
  DictRegionExportReq,
  DictRegionImportReq,
} from "./types";

class DictRegionApiImpl implements DictRegionApi {
  getKey(model: DictRegionModel): DictRegionKey {
    return {
      regionId: model.regionId,
    } as DictRegionKey;
  }

  // 查询列表信息地区信息
  findList(query?: DictRegionReq): AxiosPromise<DictRegionList> {
    const tenant = useUserStore().user.tenant;
    if (query) {
      query.hasEnable = query?.enable !== undefined;
      query.hasParent = false;
      if (query?.enable === false || query?.likeRegionName) {
        query.hasParent = true;
      }
    }
    clearEmpty(query);
    return request({
      url: "/v1/region/dict_region/list/" + tenant,
      method: "get",
      params: query,
    });
  }

  // 根据地区编码查询地区信息
  findByCode(query: DictRegionReq): AxiosPromise<DictRegionModel> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: `/v1/region/dict_region/code/${tenant}/${query.regionCode}`,
      method: "get",
      params: query,
    });
  }

  // 查询地区信息
  findById(query: DictRegionKey): AxiosPromise<DictRegionModel> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/region/dict_region/id/" + tenant + "/" + query.regionId,
      method: "get",
    });
  }

  // 创建实体地区信息
  create(data: DictRegionCreateReq): AxiosPromise<DictRegionModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/region/dict_region",
      method: "post",
      data: data,
    });
  }

  // 修改实体地区信息
  update(data: DictRegionUpdateReq): AxiosPromise<DictRegionModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/region/dict_region",
      method: "patch",
      data: data,
    });
  }

  // 上移下移
  move(key: DictRegionKey, up: boolean): AxiosPromise<boolean> {
    let data: DictRegionMoveKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.up = up;
    clearEmpty(data);
    return request({
      url: "/v1/region/dict_region/move",
      method: "put",
      data: data,
    });
  }

  // 启用/禁用
  enable(key: DictRegionKey, enable: boolean): AxiosPromise<boolean> {
    let data: DictRegionEnableKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.enable = enable;
    clearEmpty(data);
    return request({
      url: "/v1/region/dict_region/enable",
      method: "put",
      data: data,
    });
  }

  // 删除地区信息
  delete(query: DictRegionKey): AxiosPromise<DictRegionModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/region/dict_region/id",
      method: "delete",
      params: query,
    });
  }

  // 下载导入模板地区信息
  downloadTemplate(): AxiosPromise {
    const tenant = useUserStore().user.tenant;
    return request({
      url: "/v1/region/dict_region/" + tenant + "/template.xlsx",
      method: "get",
      responseType: "blob",
      params: {
        tenant: useUserStore().user.tenant,
      },
    });
  }

  // 导入地区信息
  importExcel(data: DictRegionImportReq): AxiosPromise<string> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/region/dict_region/import",
      method: "patch",
      data: data,
    });
  }

  // 导出地区信息Excel文件
  exportExcel(queryParams: DictRegionExportReq): AxiosPromise {
    const tenant = useUserStore().user.tenant;
    clearEmpty(queryParams);
    queryParams.req.hasParent = true;
    const query = qs.stringify(queryParams, {
      arrayFormat: "brackets",
      allowDots: true,
    });
    return request({
      url: "/v1/region/dict_region/" + tenant + "/dict_regions.xlsx?" + query,
      method: "get",
      responseType: "blob",
    });
  }
}
const dictRegionApi = new DictRegionApiImpl();

export { dictRegionApi };
